<?php

namespace app\admin\controller;

use think\Controller;
use Util\SysDb;
use think\Db;

class Logistics extends Controller
{
    public function stopareaalllist()
    {
        $this->db = new SysDb();
        $one = $this->db->table('goods_store')->lists();
        exit(json_encode(['message' => 'succ', 'result' => $one]));
    }

    public function stoparealist()
    {
       
        $storeId = trim(input('param.storeId'));
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $where=array(
            "store_id" =>$storeId,

        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $list = DB::name("sys_stop_area")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('sys_stop_area')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function stopareadel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $count = $this->db->table('sys_stop_area')->where(['id' =>  $id])->delete();
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function stopareaupdate()
    {
        $areaType = trim(input('param.areaType'));
        $storeId = trim(input('param.storeId'));
        $provinceName = trim(input('param.provinceName'));
        $cityName = trim(input('param.cityName'));
        $expAreaName = trim(input('param.expAreaName'));
        $reason = trim(input('param.reason'));
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $count = $this->db->table('sys_stop_area')->where(['id' =>  $id])->item();
        $data['area_type'] = $areaType;
        $data['store_id'] = $storeId;
        $data['province_name'] = $provinceName;
        $data['city_name'] = $cityName;
        $data['exp_area_name'] = $expAreaName;
        $data['reason'] = $reason;
        $count = $this->db->table('sys_stop_area')->where(['id' =>  $id])->update($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }
    public function stopareaupadd()
    {
        $areaType = trim(input('param.areaType'));
        $storeId = trim(input('param.storeId'));
        $provinceName = trim(input('param.provinceName'));
        $cityName = trim(input('param.cityName'));
        $expAreaName = trim(input('param.expAreaName'));
        $reason = trim(input('param.reason'));
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $data['area_type'] = $areaType;
        $data['store_id'] = $storeId;
        $data['province_name'] = $provinceName;
        $data['city_name'] = $cityName;
        $data['exp_area_name'] = $expAreaName;
        $data['reason'] = $reason;
        $count = $this->db->table('sys_stop_area')->where(['id' =>  $id])->insert($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function expressfeealllist()
    {
        $this->db = new SysDb();
        $one = $this->db->table('goods_store')->lists();
        exit(json_encode(['message' => 'succ', 'result' => $one]));
    }
    public function expressfeelist()
    {
        $storeId  = trim(input('param.storeId'));
        $expressSheetId  = trim(input('param.expressSheetId'));
        $where=array(
            
            "store_id" => $storeId,
            "express_sheet_id" => $expressSheetId
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = Db::table('sys_express_sheet')
            ->alias('ses')
            ->join('sys_express_fee sef', 'sef.express_sheet_id = ses.id', "left")
            ->where($where)
            ->limit($tol, $limit)
            
            ->select();
        $count = Db::table('sys_express_sheet')
            ->alias('ses')
            ->join('sys_express_fee sef', 'sef.express_sheet_id = ses.id', "left")
            ->where($where)
            ->limit($tol, $limit)
            ->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function expresssheetalllist()
    {
        $id = trim(input('param.storeId'));
        $this->db = new SysDb();
        $one = $this->db->table('sys_express_sheet')->where(['store_id' =>  $id])->lists();
        exit(json_encode(['message' => 'succ', 'result' => $one]));
    }
    
    public function expressfeedel()
    {   
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $one = $this->db->table('sys_express_fee')->where(['id' =>  $id])-> delete();
        if($one){
            exit(json_encode(['message' => 'succ', 'result' =>"操作成功"]));
        }
       
    }

    public function expressfeeupdate()
    {
        $storeId = trim(input('param.storeId'));
        $expressSheetId = trim(input('param.expressSheetId'));
        $maxWeight = trim(input('param.maxWeight'));
        $cost = trim(input('param.cost'));
        $tenantCost = trim(input('param.tenantCost'));
        $remark = trim(input('param.remark'));
        $id = trim(input('param.id'));
        $otherType = trim(input('param.otherType'));
        $this->db = new SysDb();
        $data = $this->db->table('sys_express_fee')->where(['id' =>  $id])->item();
    //    $data['store_id']=$storeId;
       $data['express_sheet_id']=$expressSheetId;
       $data['max_weight']=$maxWeight;
       $data['cost']=$cost;
       $data['tenant_cost']=$tenantCost;
       $data['remark']=$remark;
       $data['id']=$id;
        $count = $this->db->table('sys_express_fee')->where(['id' =>  $id])->update($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function expressfeeadd()
    {
        $storeId = trim(input('param.storeId'));
        $expressSheetId = trim(input('param.expressSheetId'));
        $maxWeight = trim(input('param.maxWeight'));
        $cost = trim(input('param.cost'));
        $tenantCost = trim(input('param.tenantCost'));
        $remark = trim(input('param.remark'));
        $id = trim(input('param.id'));
        $otherType = trim(input('param.otherType'));
        $this->db = new SysDb();
      
    //    $data['store_id']=$storeId;
       $data['express_sheet_id']=$expressSheetId;
       $data['max_weight']=$maxWeight;
       $data['cost']=$cost;
       $data['tenant_cost']=$tenantCost;
       $data['remark']=$remark;
       $data['other_type']=1;
       $data['id']=$id;
        $count = $this->db->table('sys_express_fee')->where(['id' =>  $id])->insert($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }
 
    public function expresssheetalllists()
    {
      
        $this->db = new SysDb();
        $one = $this->db->table('goods_store')->lists();
        exit(json_encode(['message' => 'succ', 'result' => $one]));
    }
    public function expresssheetlist()
    {
        $expressStatus  = trim(input('param.expressStatus'));
        $shipperName  = trim(input('param.shipperName'));
        $storeId  = trim(input('param.storeId'));
        $where=array(
            "express_status" => $expressStatus,
            "shipper_name" => $shipperName,
            "store_id" => $storeId
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("sys_express_sheet")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('sys_express_sheet')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function expresssheetdel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $count = $this->db->table('sys_express_sheet')->where(['id' =>  $id])->delete();
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function expresssheetupdate()
    {
        $userId = trim(input('param.user_id'));
        $storeId = trim(input('param.store_id'));
        $expressStatus = trim(input('param.express_status'));
        $customerPwd = trim(input('param.customer_pwd'));
        $customerName = trim(input('param.customer_name'));
        $shipperCode = trim(input('param.shipper_code'));
        $shipperName = trim(input('param.shipper_name'));
        $company = trim(input('param.company'));
        $name = trim(input('param.name'));
        $mobile = trim(input('param.mobile'));
        $id = trim(input('param.id'));
        $tel = trim(input('param.tel'));
        $provinceName = trim(input('param.province_name'));
        $cityName = trim(input('param.city_name'));
        $expAreaName = trim(input('param.exp_area_name'));
        $address = trim(input('param.address'));
        $otherId = trim(input('param.other_id'));
        $expressType = trim(input('param.express_type'));
        $otherType = trim(input('param.other_type'));
        $this->db = new SysDb();
        $data = $this->db->table('sys_express_sheet')->where(['id' =>  $id])->item();
    //    $data['store_id']=$storeId;
       $data['user_id']=$userId;
       $data['store_id']=$storeId;
       $data['express_status']=$expressStatus;
       $data['customer_pwd']=$customerPwd;
       $data['customer_name']=$customerName;
       $data['id']=$id;
       $data['shipper_code']=$shipperCode;
       $data['company']=$company;
       $data['shipper_name']=$shipperName;
       $data['name']=$name;
       $data['mobile']=$mobile;
       $data['tel']=$tel;
       $data['province_name']=$provinceName;
       $data['city_name']=$cityName;
       $data['exp_area_name']=$expAreaName;
       $data['address']=$address;
       $data['other_id']=$otherId;
       $data['express_type']=$expressType;
       $data['other_type']=$otherType;
        $count = $this->db->table('sys_express_sheet')->where(['id' =>  $id])->update($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function expresssheetadd()
    {
        $userId = trim(input('param.user_id'));
        $storeId = trim(input('param.store_id'));
        $expressStatus = trim(input('param.express_status'));
        $customerPwd = trim(input('param.customer_pwd'));
        $customerName = trim(input('param.customer_name'));
        $shipperCode = trim(input('param.shipper_code'));
        $shipperName = trim(input('param.shipper_name'));
        $company = trim(input('param.company'));
        $name = trim(input('param.name'));
        $mobile = trim(input('param.mobile'));
        $id = trim(input('param.id'));
        $tel = trim(input('param.tel'));
        $provinceName = trim(input('param.province_name'));
        $cityName = trim(input('param.city_name'));
        $expAreaName = trim(input('param.exp_area_name'));
        $address = trim(input('param.address'));
        $otherId = trim(input('param.other_id'));
        $expressType = trim(input('param.express_type'));
        $otherType = trim(input('param.other_type'));
        $this->db = new SysDb();
       $data['user_id']=$userId;
       $data['store_id']=$storeId;
       $data['express_status']=$expressStatus;
       $data['customer_pwd']=$customerPwd;
       $data['customer_name']=$customerName;
    //    $data['id']=$id;
       $data['shipper_code']=$shipperCode;
       $data['company']=$company;
       $data['shipper_name']=$shipperName;
       $data['name']=$name;
       $data['mobile']=$mobile;
       $data['tel']=$tel;
       $data['province_name']=$provinceName;
       $data['city_name']=$cityName;
       $data['exp_area_name']=$expAreaName;
       $data['address']=$address;
       $data['other_id']=$otherId;
       $data['express_type']=$expressType;
       $data['other_type']=$otherType;
        $count = $this->db->table('sys_express_sheet')->insert($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function storelist()
    {
        $name = trim(input('param.name'));
        $where=array(
            "name" => $name
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("goods_store")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('goods_store')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function storedel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $count = $this->db->table('goods_store')->where(['id' =>  $id])->delete();
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function storeupdate()
    {
        $name = trim(input('param.name'));
        $address = trim(input('param.address'));
        $remark = trim(input('param.remark'));
        $status = trim(input('param.status'));
        $sort = trim(input('param.sort'));
        $tbStatus = trim(input('param.tbStatus'));
        $pddStatus = trim(input('param.pddStatus'));
        $douyinStatus = trim(input('param.douyinStatus'));
        $stopStatus = trim(input('param.stopStatus'));
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $data = $this->db->table('goods_store')->where(['id' =>  $id])->item();
    //    $data['store_id']=$storeId;
       $data['remark']=$remark;
       $data['status']=$status;
       $data['sort']=$sort;
       $data['tb_status']=$tbStatus;
       $data['pdd_status']=$pddStatus;
       $data['id']=$id;
       $data['douyin_status']=$douyinStatus;
       $data['stop_status']=$stopStatus;
       $data['name']=$name;
       $data['address']=$address;
        $count = $this->db->table('goods_store')->where(['id' =>  $id])->update($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function storeadd()
    {
        $name = trim(input('param.name'));
        $address = trim(input('param.address'));
        $remark = trim(input('param.remark'));
        $status = trim(input('param.status'));
        $sort = trim(input('param.sort'));
        $tbStatus = trim(input('param.tbStatus'));
        $pddStatus = trim(input('param.pddStatus'));
        $douyinStatus = trim(input('param.douyinStatus'));
        $stopStatus = trim(input('param.stopStatus'));
        $id = trim(input('param.id'));
        $this->db = new SysDb();
      
    //    $data['store_id']=$storeId;
       $data['remark']=$remark;
       $data['status']=$status;
       $data['sort']=$sort;
       $data['tb_status']=$tbStatus;
       $data['pdd_status']=$pddStatus;
       $data['id']=$id;
       $data['douyin_status']=$douyinStatus;
       $data['stop_status']=$stopStatus;
       $data['name']=$name;
       $data['address']=$address;
        $count = $this->db->table('goods_store')->insert($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }
}
